package com.mDigitalHealth.service;

import org.apache.commons.lang3.StringUtils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.mDigitalHealth.common.Utils;
import com.mDigitalHealth.database.DataType;
import com.mDigitalHealth.database.DatabaseHelper;
import com.mDigitalHealth.database.Store;

/**
 * 
 * <pre>
 * 业务名:注册服务类
 * 功能说明: 
 * 编写日期:	2013-1-14
 * 作者:	Administrator
 * 
 * </pre>
 */
public class RegService {

	/**
	 * 
	 * 方法说明：写入注册信息
	 * 
	 * @param context
	 *            上下文，调用方法的activity
	 * @return
	 */
	public Long insertUserInfor(Context context) {

		DatabaseHelper mDatabaseHelper = new DatabaseHelper(context);
		// android.database.sqlite.SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
		Long resulte = null;
		try {
			ContentValues values = new ContentValues();

			values.put(Store.UserInfoColumns.LOGIN_NAME, Utils.getIMEI(context));
			values.put(Store.UserInfoColumns.REGFLAG, "1");

			// Calendar mCalendar = Calendar.getInstance();
			// mCalendar.set(Calendar.MINUTE, 0);
			// values.put(Store.UserInfoColumns.CREATE_TIME, mCalendar.getTimeInMillis());
			// values.put(Store.UserInfoColumns.LOGIN_NAME, "");

			resulte = mDatabaseHelper.insert(DataType.USER_INFO, values);

		} catch (Exception e) {

		} finally {
			mDatabaseHelper.close();
			mDatabaseHelper = null;
		}
		return resulte;

	}

	/**
	 * 
	 * 方法说明：查询注册标志位
	 * 
	 * @param context
	 *            上下文，调用方法的activity
	 */
	public String queryRegFlag(Context context) {
		DatabaseHelper mDatabaseHelper = new DatabaseHelper(context);
		String resulte = null;
		try {

			Cursor cursor = mDatabaseHelper.query(DataType.USER_INFO, "loginName=?",
					new String[] { Utils.getIMEI(context) }, null, null, null);
			while (cursor.moveToNext()) {
				resulte = cursor.getString(cursor.getColumnIndex(Store.UserInfoColumns.REGFLAG));
				// 获取值后跳出
				if (StringUtils.isNotEmpty(resulte)) {
					break;
				}
				System.out.println("query--->" + resulte);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			mDatabaseHelper.close();
			mDatabaseHelper = null;
		}
		return resulte;
	}
}
